%% writeDicoms
% Reads in image files and writes them to disk as dicom
%
%% Syntax
% [  ] = writeDicoms( pathsAndNames, saveLocation )
%
% [  ] = writeDicoms( {'D:\ImageFolder\foo.jpg', 'foo'} , 'D:\SaveLocation')
%
% [  ] = writeDicoms( {'D:\ImageFolder\foo.jpg', 'foo'; 'D:\ImageFolder\foo.bar', 'bar'} , 'D:\SaveLocation')
%
%% Description
% This function reads in image files specified by full paths. It then
% converts the image files to dicoms and writes them back to the file
% system in the specified save directory.
%
%% Arguments
%* pathsAndNames             - Cell: Full paths to image files of interest (col
%                                    1) and the names of the image files (col 2) 
%
%* saveLocation              - Char: The path to the desired folder save
%                                    location
%
%% Returns
%* N/A
%
%% Function Side Effects
% * Dicom files are saved to the file system
%
%% Exceptions
%* An error is thrown if pathsAndNames is empty or is not of type cell
% 
%* An error is thrown if pathsAndNames is not of size n by 2
%
%* An error is thrown if saveLocation is empty or is not of type char
%
%* An error is thrown if saveLocation does not refer to a valid directory
%
%% See Also
% selectDirectory
% makeFullPaths
% filterByExtension

function [  ] = writeDicoms( pathsAndNames, saveLocation )

iP = inputParser;
iP.FunctionName = 'writeDicoms';

pathsSize = size(pathsAndNames);

% Checks whether paths / names is of type cell, isn't empty, and is n by 2
pathsNamesValidationFcn = @(x) iscell(x) && isempty(x) == 0 && pathsSize(2) == 2;

% Checks that the save path is of type char and points to an actual
% directory
saveLocValidationFcn = @(x) ischar(x) && isempty(x) == 0 && exist(x, 'dir') == 7;

iP.addRequired('pathsAndNames', pathsNamesValidationFcn);
iP.addRequired('saveLocation', saveLocValidationFcn);

try 
    
    iP.parse(pathsAndNames, saveLocation);

catch ME
    
    errorMsg = ['pathsAndNames argument must be a n by 2 cell and cannot be empty\n', ...
                'saveLocation argument must be of type character, cannot be empty\n', ... 
                'and must point to an existing directory'];
    
    error('writeDicoms:invalidFunctionArguments', errorMsg);
    
end

% Imports a single image file, converts to dicom, and saved to disk
for i = 1
    
    imIdx = pathsAndNames{i, 1};
    titleIdx = pathsAndNames{i, 2};
    
    imageToWrite = imread(imIdx);
    
    dicomwrite(imageToWrite, strcat(saveLocation, '\', titleIdx, '.dcm'));
end


end
